<?php
include 'PublicdetailController.php';
class CatalogController extends PublicdetailController {
	private $_MCatalog;
	private $_Catalog = array ();
	private $_fomatgia;
    public $idnguoidung;
    public $nguoidung;
    public function init() {
		   $this->initValue ();
		   $this->idnguoidung = $this->TblUserbyId[0]['id'];
           $this->nguoidung = $this->TblUserbyId[0]['name'];
		   $this->_MCatalog = new Model_Catalog();		
		   $this->_Catalog = array (
					'name' => @$_REQUEST ['name'], 
					'trocap' => @$_REQUEST ['trocap'],
					'ngaynghi' => @$_REQUEST ['ngaynghi'],
					'luongngay' => @$_REQUEST ['luongngay'],
					'luong' => @$_REQUEST ['luong'],
					'ghichu' => @$_REQUEST ['ghichu'],
				    'createdDate' =>  date('Y-m-d H:i:s'),   
					'tamung' => @$_REQUEST ['tamung'],
			);
		}

    public function indexAction() {
    if($this->TblUserbyId[0]['roles']!='Admin' && $this->TblUserbyId[0]['roles']!='Quanly')
		{
		echo "<script type=\"text/javascript\">
			    alert('Bạn không có quyền vào mục này!');
				 window.history.back();
			</script>";
		die;
		}

      }      
     public function jsonAction() {
		$this->_helper->layout ()->disableLayout ();
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$sort = isset ( $_POST ['sort'] ) ? strval ( $_POST ['sort'] ) : 'name';
		$order = isset ( $_POST ['order'] ) ? strval ( $_POST ['order'] ) : 'asc';
		$offset = ($page - 1) * $rows;
		$jsonObj = json_encode($this->_MCatalog->getItems($sort, $order, $offset, $rows));        
		return $this->view->items = $jsonObj;
		
	}
	
	 public function jsondsAction() {
		$this->_helper->layout ()->disableLayout ();
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$sort = isset ( $_POST ['sort'] ) ? strval ( $_POST ['sort'] ) : 'name';
		$order = isset ( $_POST ['order'] ) ? strval ( $_POST ['order'] ) : 'asc';
		$offset = ($page - 1) * $rows;
		$jsonObj = json_encode($this->_MCatalog->getItemsds($sort, $order, $offset, $rows));        
		echo $jsonObj;
		die;
		
	}
	public function thanhtoanAction() {
		$data =  @$_REQUEST['data'];
		$id = $data['id'];
		$data['ids'] =$id;
		$data['userid'] =  $this->idnguoidung;
		$data['nameuser'] = $this->nguoidung;
		$data['created'] = date('Y-m-d H:i:s');
		$ketqua = array (
					'ngaynghi' => 0,
                    'tamung'	=> 0,
					'createdDate' => date('Y-m-d H:i:s')
			);
		$this->_helper->layout ()->disableLayout ();
		if($id){
			$this->_MCatalog->thanhtoans( $id, $ketqua );
			$this->_MCatalog->addObj($data);
		}
		return $this->view->items = json_encode ( $jsonObj );
		
	}
	
	public function updateAction() {      
		$id =  @$_REQUEST['id'];
		$this->_helper->layout ()->disableLayout ();
		if($id){
			$this->_MCatalog->update( $id, $this->_Catalog );
		}
		return $this->view->items = json_encode ( $jsonObj );
		
	}
	
	public function updatedsAction() {      
		$id =  @$_REQUEST['id'];
		$data['userid'] =  $this->idnguoidung;
		$data['nameuser'] = $this->nguoidung;
		$this->_helper->layout ()->disableLayout ();
		$dsl = array (
					'name' => @$_REQUEST ['name'], 
					'trocap' => @$_REQUEST ['trocap'],
					'ngaynghi' => @$_REQUEST ['ngaynghi'],
					'luongngay' => @$_REQUEST ['luongngay'],
					'luong' => @$_REQUEST ['luong'],
					'ghichu' => @$_REQUEST ['ghichu'],
				    'created' =>  date('Y-m-d H:i:s'),   
					'tamung' => @$_REQUEST ['tamung'],
					'userid' => $this->idnguoidung,
					'nameuser' => $this->nguoidung,
			);
		if($id){
			$this->_MCatalog->updateds( $id, $dsl);
		}
		die;
		
	}
	
	public function delAction() {
		$this->_helper->layout ()->disableLayout ();
		//$id = @$_REQUEST ['id'];
	    $id = $this->_getParam ( 'id' );
		$_Id = null;
		$jsonObjv = null;
		
		if (isset ( $id ) && $id > 0) {
			$_Id = $this->_MCatalog->del( $id );
		} else {
			return;
		}
		
		if (! $_Id) {
			$jsonObj = json_encode ( array (
					'success' => false 
			) );
		} else {
			$jsonObj = json_encode ( array (
					'success' => true 
			) );
		}
		die;
	}
	public function preDispatch() {
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			if ($this->_request->getActionName () != 'login') {
				$this->_redirect ( '/index/login' );
			}
		}
	}
	
}